package com.yq.controller;

import com.yq.bean.ClockWork;
import com.yq.bean.Clock_info;
import com.yq.bean.Employee;
import com.yq.bean.Notification;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

public class EmployeePart {
    //登录
    public static Integer loginEmployee(ObjectOutputStream oos, ObjectInputStream ois) {
        // --实现控制台和用户的交互：【主人登录：读取主人的账号、密码】
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入账号");
        String loginName = sc.next();
        System.out.println("请输入密码");
        String pw = sc.next();
        // --发送请求：可以使用一个方法来封装
        // 给服务端发送Notification数据【action、map】
        Notification notification = new Notification();
        //设置action、service、map，boolean不需要（给服务器提供的）
        notification.setAction("loginEmployee");
        notification.setService("EmployeeService");
        HashMap<String, Object> map = new HashMap<>();
        map.put("loginName", loginName);
        map.put("pw", pw);
        notification.setMap(map);

        try {
            //发送Notification
            oos.writeObject(notification);
            //等待服务器处理后，接收服务器返回的数据，转成Notification
            notification = (Notification) ois.readObject();
            //根据操作是否成功，执行不同操作
            if (notification.isFlag()) {
                System.out.println(notification.getMap().get("employee_name") + " 登陆成功");
            } else {
                System.out.println(notification.getMap().get("message"));
                //重新调用
                //不能调用本方法（递归），会出现空指针异常
                Client.menu();
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        //返回id（员工主键）
        return (Integer) notification.getMap().get("employee_id");
    }

    //由id get角色
    public static Integer getRole(ObjectOutputStream oos, ObjectInputStream ois, Integer e_id) {
        Notification notification = new Notification();
        notification.setAction("getRole");
        notification.setService("EmployeeService");
        HashMap<String, Object> map = new HashMap<>();
        map.put("e_id", e_id);
        notification.setMap(map);

        try {
            oos.writeObject(notification);
            notification = (Notification) ois.readObject();

            if (!notification.isFlag()) {
                System.out.println(notification.getMap().get("message"));
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        //返回
        return (Integer) notification.getMap().get("role");
    }

    //由id get号码
    public static String getNo(ObjectOutputStream oos, ObjectInputStream ois, Integer e_id) {
        Notification notification = new Notification();
        notification.setAction("getNo");
        notification.setService("EmployeeService");
        HashMap<String, Object> map = new HashMap<>();
        map.put("e_id", e_id);
        notification.setMap(map);

        try {
            //执行发送
            oos.writeObject(notification);
            //等待服务器处理后，接收服务器返回的数据
            notification = (Notification) ois.readObject();

            if (!notification.isFlag()) {
                System.out.println(notification.getMap().get("message"));
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        //返回
        return (String) notification.getMap().get("e_no");
    }

    //注册
    public static void registerEmployee(ObjectOutputStream oos, ObjectInputStream ois) {
        // 读取输入信息，放到一个Employee里
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入员工名");
        String employee_name = sc.nextLine();
        System.out.println("请输入员工编号");
        String employee_no = sc.nextLine();
        System.out.println("请输入登录名");
        String login_name = sc.nextLine();
        System.out.println("请输入登录密码");
        String pass_word = sc.nextLine();
        System.out.println("请输入工作职位");
        String job = sc.nextLine();
        System.out.println("入职时间");
        Date hiredayte = Date.valueOf(sc.nextLine());
        System.out.println("月薪");
        double sal = Double.parseDouble(sc.nextLine());
        Employee newEmployee = new Employee(employee_no, employee_name, login_name, pass_word, job, hiredayte, sal, 0);

        Notification notification = new Notification();
        notification.setAction("registerEmployee");
        notification.setService("EmployeeService");
        HashMap<String, Object> map = new HashMap<>();
        map.put("newEmployee", newEmployee);
        notification.setMap(map);

        try {
            oos.writeObject(notification);
            notification = (Notification) ois.readObject();

            if (notification.isFlag()) {
                System.out.println("注册成功");
            } else {
                System.out.println(notification.getMap().get("message"));
                //重新调用
                Client.menu();
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    //上班打卡
    public static void inTimeClock(ObjectOutputStream oos, ObjectInputStream ois, String e_no) {
        //新建一个打卡记录对象
        Clock_info ci = new Clock_info(e_no);
        //set当前时间和日期
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        String s1 = sdf1.format(calendar.getTime());
        String s2 = sdf2.format(calendar.getTime());
        ci.setClock_off_time(s1);
        ci.setClock_date(s2);
        ci.setClock_in_time(s1);
        ci.setClock_date(s2);

//        java.util.Date date = new java.util.Date();
//        Timestamp t = new Timestamp(date.getTime());

        Notification notification = new Notification();
        notification.setAction("in_time_Clock");
        notification.setService("EmployeeService");
        HashMap<String, Object> map = new HashMap<>();
        map.put("in_clock", ci);
        notification.setMap(map);

        try {
            oos.writeObject(notification);
            notification = (Notification) ois.readObject();

            if (notification.isFlag()) {
                System.out.println("上班打卡成功");
            } else {
                System.out.println(notification.getMap().get("message"));
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    //下班打卡
    public static void offTimeClock(ObjectOutputStream oos, ObjectInputStream ois, String e_no) {
        Clock_info ci = new Clock_info(e_no);
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        String s1 = sdf1.format(calendar.getTime());
        String s2 = sdf2.format(calendar.getTime());
        ci.setClock_off_time(s1);
        ci.setClock_date(s2);

        Notification notification = new Notification();
        notification.setAction("off_time_Clock");
        notification.setService("EmployeeService");
        HashMap<String, Object> map = new HashMap<>();
        map.put("off_clock", ci);
        notification.setMap(map);

        try {
            //执行发送
            oos.writeObject(notification);
            //等待服务器处理后，接收服务器返回的数据
            notification = (Notification) ois.readObject();

            if (notification.isFlag()) {
                System.out.println("下班打卡成功");
            } else {
                System.out.println(notification.getMap().get("message"));
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    //查看个人考勤信息
    public static void ClockWork(ObjectOutputStream oos, ObjectInputStream ois, String e_no) {
        Notification notification = new Notification();
        notification.setAction("selectClock");
        notification.setService("EmployeeService");
        HashMap<String, Object> map = new HashMap<>();
        map.put("e_no", e_no);
        notification.setMap(map);
        //统计：迟到、正常、旷工、早退次数
        int zhengchang = 0;
        int chidao = 0;
        int zaotui = 0;
        int kuanggong = 0;

        try {
            oos.writeObject(notification);
            notification = (Notification) ois.readObject();

            if (notification.isFlag()) {
                System.out.println("查询成功");
                //返回list
                List<ClockWork> clockWorks = (List<ClockWork>) notification.getMap().get("clockWorks");
                System.out.println("员工号\t姓名\t\t日期\t\t\t上班打卡时间：\t\t\t上班状态\t下班打卡时间\t\t\t\t下班状态");
                //遍历输出
                for (ClockWork clockWork : clockWorks) {
                    switch (clockWork.getDiff_in_status()) {
                        case "正常":
                            zhengchang++;
                            break;
                        case "迟到":
                            chidao++;
                            break;
                        case "旷工":
                        case "忘记打卡":
                            kuanggong++;
                            break;
                    }
                    switch (clockWork.getDiff_off_status()) {
                        case "正常":
                            zhengchang++;
                            break;
                        case "早退":
                            zaotui++;
                            break;
                        case "旷工":
                        case "忘记打卡":
                            kuanggong++;
                            break;
                    }
                    System.out.println(clockWork.getEmploy_no() +"\t"+
                            clockWork.getEmploy_name() +" \t"+
                            clockWork.getClock_date() +"\t"+
                            clockWork.getClock_in_time() +"\t"+ clockWork.getDiff_in_status() +"\t\t"+
                            clockWork.getClock_off_time() +"\t"+  clockWork.getDiff_off_status());
                }
                System.out.println("统计：正常次数:" + zhengchang + "\t迟到次数:" + chidao + "\t早退次数:" + zaotui + "\t旷工次数:" + kuanggong);
            } else {
                System.out.println(notification.getMap().get("message"));
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    /*//根据no模糊搜索个人考勤信息
    public static void ClockWorkNo(ObjectOutputStream oos, ObjectInputStream ois) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要搜索的员工编号：");
        String e_no = sc.next();
        Notification notification = new Notification();
        notification.setAction("selectClockNo");
        notification.setService("EmployeeService");
        HashMap<String, Object> map = new HashMap<>();
        map.put("e_no", e_no);
        notification.setMap(map);

        try {
            //执行发送
            oos.writeObject(notification);
            //等待服务器处理后，接收服务器返回的数据
            notification = (Notification) ois.readObject();

            if (notification.isFlag()) {
                System.out.println("查询成功");
                List<ClockWork> clockWorks = (List<ClockWork>) notification.getMap().get("clockWorks");
                for (ClockWork clockWork : clockWorks) {
                    System.out.println("员工号："+clockWork.getEmploy_no()+
                            "\t姓名："+clockWork.getEmploy_name()+
                            "\t日期:"+clockWork.getClock_date()+
                            "\t上班打卡时间："+clockWork.getClock_in_time()+"\t"+clockWork.getDiff_in_status()+
                            "\t下班打卡时间："+clockWork.getClock_off_time()+"\t"+clockWork.getDiff_off_status());
                }
            } else {
                System.out.println(notification.getMap().get("message"));
                //重新调用
//                ClockWork(oos, ois, e_no);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    //根据name模糊搜索个人考勤信息
    public static void ClockWorkName(ObjectOutputStream oos, ObjectInputStream ois) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要搜索的员工姓名：");
        String e_no = sc.next();
        Notification notification = new Notification();
        notification.setAction("selectClockName");
        notification.setService("EmployeeService");
        HashMap<String, Object> map = new HashMap<>();
        map.put("e_name", e_no);
        notification.setMap(map);

        try {
            //执行发送
            oos.writeObject(notification);
            //等待服务器处理后，接收服务器返回的数据
            notification = (Notification) ois.readObject();

            if (notification.isFlag()) {
                System.out.println("查询成功");
                List<ClockWork> clockWorks = (List<ClockWork>) notification.getMap().get("clockWorks");
                for (ClockWork clockWork : clockWorks) {
                    System.out.println("员工号："+clockWork.getEmploy_no()+
                            "\t姓名："+clockWork.getEmploy_name()+
                            "\t日期:"+clockWork.getClock_date()+
                            "\t上班打卡时间："+clockWork.getClock_in_time()+"\t"+clockWork.getDiff_in_status()+
                            "\t下班打卡时间："+clockWork.getClock_off_time()+"\t"+clockWork.getDiff_off_status());
                }
            } else {
                System.out.println(notification.getMap().get("message"));
                //重新调用
//                ClockWork(oos, ois, e_no);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    //根据date模糊搜索个人考勤信息
    public static void ClockWorkDate(ObjectOutputStream oos, ObjectInputStream ois) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要搜索的日期：（如2021-07）");
        String w_date = sc.next();
        Notification notification = new Notification();
        notification.setAction("selectClockDate");
        notification.setService("EmployeeService");
        HashMap<String, Object> map = new HashMap<>();
        map.put("w_date", w_date);
        notification.setMap(map);

        try {
            //执行发送
            oos.writeObject(notification);
            //等待服务器处理后，接收服务器返回的数据
            notification = (Notification) ois.readObject();

            if (notification.isFlag()) {
                System.out.println("查询成功");
                List<ClockWork> clockWorks = (List<ClockWork>) notification.getMap().get("clockWorks");
                for (ClockWork clockWork : clockWorks) {
                    System.out.println("员工号："+clockWork.getEmploy_no()+
                            "\t姓名："+clockWork.getEmploy_name()+
                            "\t日期:"+clockWork.getClock_date()+
                            "\t上班打卡时间："+clockWork.getClock_in_time()+"\t"+clockWork.getDiff_in_status()+
                            "\t下班打卡时间："+clockWork.getClock_off_time()+"\t"+clockWork.getDiff_off_status());
                }
            } else {
                System.out.println(notification.getMap().get("message"));
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }*/

    //模糊搜索考勤信息（可根据时间、名字、号码）
    public static void ClockWorkAll(ObjectOutputStream oos, ObjectInputStream ois) {
        Scanner sc = new Scanner(System.in);
        System.out.println("如果不输入代表不限制该条件，支持模糊查询");
        System.out.println("请输入员工编号：");
        String e_no = sc.nextLine();//因为回车代表不设条件，所以nextLine()不是next()
        System.out.println("请输入员工名");
        String e_name = sc.nextLine();
        System.out.println("请输入年月");
        String w_date = sc.nextLine();
        Notification notification = new Notification();
        notification.setAction("selectClockAll");
        notification.setService("EmployeeService");
        HashMap<String, Object> map = new HashMap<>();
        map.put("w_date", w_date);
        map.put("e_name", e_name);
        map.put("e_no", e_no);
        notification.setMap(map);
        List<ClockWork> clockWorks;
        //统计：迟到、正常、旷工、早退次数
        int zhengchang = 0;
        int chidao = 0;
        int zaotui = 0;
        int kuanggong = 0;

        try {
            oos.writeObject(notification);
            notification = (Notification) ois.readObject();

            if (notification.isFlag()) {
                System.out.println("查询成功");
                clockWorks = (List<ClockWork>) notification.getMap().get("clockWorks");
                System.out.println("员工号\t姓名\t\t日期\t\t\t上班打卡时间：\t\t\t上班状态\t下班打卡时间\t\t\t\t下班状态");
                for (ClockWork clockWork : clockWorks) {
                    switch (clockWork.getDiff_in_status()) {
                        case "正常":
                            zhengchang++;
                            break;
                        case "迟到":
                            chidao++;
                            break;
                        case "旷工":
                        case "忘记打卡":
                            kuanggong++;
                            break;
                    }
                    switch (clockWork.getDiff_off_status()) {
                        case "正常":
                            zhengchang++;
                            break;
                        case "早退":
                            zaotui++;
                            break;
                        case "旷工":
                        case "忘记打卡":
                            kuanggong++;
                            break;
                    }
                    System.out.println(clockWork.getEmploy_no() +"\t"+
                            clockWork.getEmploy_name() +" \t"+
                            clockWork.getClock_date() +"\t"+
                            clockWork.getClock_in_time() +"\t"+ clockWork.getDiff_in_status() +"\t\t"+
                            clockWork.getClock_off_time() +"\t"+  clockWork.getDiff_off_status());
                }
                System.out.println("统计：正常次数:" + zhengchang + "\t迟到次数:" + chidao + "\t早退次数:" + zaotui + "\t旷工次数:" + kuanggong);
            } else {
                System.out.println(notification.getMap().get("message"));
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }

    }

    //查看所有考勤信息
   /* public static void ClockWorkAll(ObjectOutputStream oos, ObjectInputStream ois) {
        Notification notification = new Notification();
        notification.setAction("selectClockAll");
        notification.setService("EmployeeService");
        HashMap<String, Object> map = new HashMap<>();
        notification.setMap(map);

        try {
            //执行发送
            oos.writeObject(notification);
            //等待服务器处理后，接收服务器返回的数据
            notification = (Notification) ois.readObject();

            if (notification.isFlag()) {
                System.out.println("查询成功");
                List<ClockWork> clockWorks = (List<ClockWork>) notification.getMap().get("clockWorks");
                for (ClockWork clockWork : clockWorks) {
                    System.out.println("员工号："+clockWork.getEmploy_no()+
                            "\t姓名："+clockWork.getEmploy_name()+
                            "\t日期:"+clockWork.getClock_date()+
                            "\t上班打卡时间："+clockWork.getClock_in_time()+"\t"+clockWork.getDiff_in_status()+
                            "\t下班打卡时间："+clockWork.getClock_off_time()+"\t"+clockWork.getDiff_off_status());
                }

            } else {
                System.out.println(notification.getMap().get("message"));
                //重新调用
//                ClockWork(oos, ois, e_no);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }*/
}
